package com.zjkj.mp.dao.user.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.zjkj.mp.dao.DaoBase;
import com.zjkj.mp.dao.user.UserDao;
import com.zjkj.mp.utils.PaginatedResult;
import com.zjkj.mp.vo.MpAuthority;
import com.zjkj.mp.vo.user.MpUser;

@Repository
public class UserDaoImpl extends DaoBase implements UserDao {

	public List<MpAuthority> getQxdmList(String zgdm) {
		// TODO Auto-generated method stub
		return null;
	}

	public MpUser loginUser(Map<String, Object> map) {
		MpUser user = (MpUser) this.getSqlMapClient().queryForObject("get.loginUser", map);
		return user;
	}

	public MpUser getMpUser(int userId) {
		MpUser user = (MpUser) this.getSqlMapClient().queryForObject("get.userInfoById", userId);
		return user;
	}

	public MpUser getMpUserByEmail(String email) {
		Object obj = this.getSqlMapClient().queryForObject("get.userInfoByEmail", email);
		if(obj != null){
			return (MpUser)obj;
		}
		return null;
	}

	public void addUser(MpUser user) {
		 this.getSqlMapClient().insert("insert.addUser", user);
	}

	public PaginatedResult  getUserList(int pageNo,int pageSize,Map<String,Object> params){
		
		if(params == null) params = new HashMap<String, Object>();
		
		PaginatedResult result = new PaginatedResult(pageNo,pageSize);
		List users =  this.getSqlMapClient().queryForList("get.userList",params, (pageNo-1)*pageSize,pageSize);
		result.setResult(users);
		
 
		int count = ((Integer)this.getSqlMapClient().queryForObject("get.userListCount",params)).intValue();
		result.setTotalSize(count);
		
		return result;
	}

	public void updateUserState(int userId,String newState) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userID", new Integer(userId));
		map.put("newState", newState);
		
		 this.getSqlMapClient().update("update.updateState", map);
	}

	public void updateInfo(MpUser user) {
		 this.getSqlMapClient().update("update.updateUserInfo",user);
	}

	@Override
	public void updateBalance(int userId,long balance) {
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("balance", balance);
		map.put("userID", userId);
		
		 this.getSqlMapClient().update("update.balance",map);
	}

	@Override
	public long getBalance(int userId) {
		System.out.println("userId" + userId);
		 return (Long) this.getSqlMapClient().queryForObject("get.balance",userId);
	}

	 
}
